/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.actions.marca;

import com.mapping.Marca;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author PARDmx
 */

@Namespace(value="/")
@Action(value="buscarMarcaP", results={@Result(name="success",location="/GestionMarcas/gestionMarca.jsp")})
public class BuscarMarcaAction extends ActionSupport{
    private String buscar;
    private List<Marca> marcas;
    
    @Override
    public String execute(){
        String consulta = "select IDMARCA,MARCA from marca where match (marca) against (?1 IN BOOLEAN MODE)"; 
        System.out.println(consulta + " busca = "+ buscar);
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("inventarios");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        if(buscar== null || buscar.equals("")){
             marcas = (List<Marca>) em.createNamedQuery("Marca.findAll").getResultList();
        }else{
            Query query =  em.createNativeQuery(consulta,Marca.class);
            query.setParameter(1,buscar+"*");          
            marcas = query.getResultList();
        }
        tx.commit();
        em.close();        
        return SUCCESS;    
    }    

    public List<Marca> getMarcas() {
        return marcas;
    }

    public void setBuscar(String buscar) {
        this.buscar = buscar;
    }
    
    
}
